package cn.wuxing.mediation.web.listener;

import cn.hutool.json.JSONUtil;
import cn.wuxing.mediation.businessplatform.listener.event.OrganizationEvent;
import cn.wuxing.mediation.web.controller.util.OrganizationEmployeeFetcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 监听事件
 */
@Slf4j
@Component
public class OrganizationEventListener {

    @Autowired
    private OrganizationEmployeeFetcher organizationEmployeeFetcher;

    /**
     * 会员代理报价：开拍自动报价
     *
     * @param organizationEvent
     */
    @Async
    @TransactionalEventListener(value = OrganizationEvent.class, fallbackExecution = true, phase = TransactionPhase.AFTER_COMMIT)
    public void proxyOffer(OrganizationEvent organizationEvent) {
        log.info("浙政钉消息发送_{}", JSONUtil.toJsonStr(organizationEvent));
        if (null == organizationEvent.getMessageNotifications()) {
            log.info("浙政钉消息发送参数异常_{}", JSONUtil.toJsonStr(organizationEvent));
            return;
        }
        this.organizationEmployeeFetcher.sendMessage(organizationEvent.getMessageNotifications());
    }

}